package com.joplus.housekeeper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.PlanBean;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by ccm on 2016/11/17 20:29
 */
public class FreeTitleAdapter extends BaseAdapter {
    LinkedHashMap<String, Integer> map;
    PlanBean planBean;
    int groupPosition;
    Context mContext;
    List<String> allDay;

    public FreeTitleAdapter(Context mContext, LinkedHashMap<String, Integer> map, PlanBean planBean, int groupPosition, List<String> allDay) {
        this.mContext = mContext;
        this.map = map;
        this.groupPosition = groupPosition;
        this.planBean = planBean;
        this.allDay = allDay;
    }

    @Override
    public int getCount() {
        return Integer.valueOf(map.get(allDay.get(groupPosition)));
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_plan_title, null);
            holder.tv1 = (TextView) convertView.findViewById(R.id.tv_1);
            holder.tv2 = (TextView) convertView.findViewById(R.id.tv_2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        int count = map.get(allDay.get(groupPosition));
        int doPosition = 0;
      for (int i = 0; i < planBean.getData().getPlanList().size(); i++) {
            PlanBean.DataBean.PlanListBean planListBean = planBean.getData().getPlanList().get(position);
            String[] split = planListBean.getTravelDate().split(" ");
            if (split[0].equals(allDay.get(groupPosition))) {
                doPosition = i;
                break;
            }
        }
        holder.tv1.setText(planBean.getData().getPlanList().get(doPosition + position).getTravelDate().split(" ")[1]);
        holder.tv2.setText(planBean.getData().getPlanList().get(doPosition + position).getTravelName());
        return convertView;
    }

    static class ViewHolder {
        TextView tv1;
        TextView tv2;
    }
}
